/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-25
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-25
 */
@Api(description = "优惠券接口")
@RestController
@RequestMapping("/coupon")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询优惠券
     *
     * @return
    */
    @ApiOperation("分页查询优惠券")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户号", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品id", dataType = "Long", paramType = "query")
    })
    public CommResult<Coupon> list(String mchId,Long productId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (productId!=null) {
            params.put("productId", productId);
        }

        Member member = memberService.getCurrent();

        if (member!=null) {
            params.put("memberId", member.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Coupon> list = couponService.selectEffectiveList(params);
        PageResult<Coupon> pageResult = new PageResult<Coupon>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条优惠券
     *
     * @return
    */
    @ApiOperation("查询单条优惠券")
    @GetMapping("/find/{id}")
    public CommResult<Coupon> find(@PathVariable String id) {
        Coupon result = couponService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("coupon",result);
        return CommResult.success(data);

    }


}
